# -*- coding: utf-8 -*-
r'''funny

just for fun
-------------------------------
Path: mywork\funny.py
Author: William/2015-07-07
'''

import math
import random


def strjoin(*x):
    return ''.join(x)

def make24(x,N=24):
    # designer: William
    # example:
    #make24([5,9,6,2],24)
    #
    #(9+(5*(6/2)))

    L=len(x)
    if L==1 and x[0]==N:
        return str(x[0])

    for k in range(L):
        xk = x[k]
        xx = x.copy()
        xx.pop(k)
        yk = make24(xx, N-xk) # yk(xx)+xk = N
        if yk != '':
            return strjoin('(',str(xk),'+',yk,')')

        yk=make24(xx, N+xk) # yk(xx)-xk=N
        if yk != '':
            return strjoin('(',yk,'-',str(xk),')')

        yk=make24(xx, -N+xk) # xk-yk(xx)=N
        if yk != '':
            return strjoin('(',str(xk),'-',yk,')')

        if xk !=0:
            yk=make24(xx, N/xk) # yk(xx)*xk=N
            if yk != '':
                return strjoin('(',str(xk),'*',yk,')')

        yk = make24(xx, N*xk) # yk(xx)/xk=N
        if yk != '':
            return strjoin('(',yk,'/',str(xk),')')

        if N !=0:
            yk=make24(xx, xk/N) # xx/yk(xx)=N
            if yk != '':
                return strjoin('(',str(xk),'/',yk,')')

    return ''

print(make24([9,7,5,4]))



# k=0
# while True:
#     a=random.randint(0,9)
#     b=random.randint(0,9)  # b=random.randint(0,10-a)
#     if a+b<=10:
#         k+=1
#         print('[%d] %d+%d=%d'%(k,a,b,a+b))
#         while True:
#             yn=input('\nto continue? [y(es)/n(o)] ')
#             if yn.lower() in {'y', 'yes'}:
#                 yn=1;break
#             elif yn.lower() in {'n','no'}:
#                 yn=0;break
#             else:
#                 continue
#         if yn==1:
#             continue
#         elif yn==0:
#             print('Game over')
#             break